import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86187
 * Date: 2022-07-19
 * Time: 16:34
 */

public class MyArrayList {

    public int[] elem;
    public int usedSize = 0;

    public static final int DEFAULT_CAPACITY = 10;

    public MyArrayList() {
        this.elem = new int[DEFAULT_CAPACITY];
    }


    //打印顺序表
    public void display() {
        int i = 0;
        for (i = 0; i < this.usedSize; ++i) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }


    //在数组最后新增一个元素:
    public void newCapacity() {
        //判断是否需要扩容
        try {
            if(this.usedSize == this.elem.length) {
                this.elem = Arrays.copyOf(elem, elem.length * 2);
            }
        } catch (NegativeArraySizeException e) {
            e.printStackTrace();
        }

    }
    public void add(int data) {
        newCapacity();//判断是否需要扩容
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }


    //在pos位置新增元素
    public void add(int pos, int data) {

        newCapacity();//判断是否需要扩容

        if(pos < 0 || pos > this.usedSize) {
            System.out.println(pos+"位置不合法！");
        } else {
            for (int i = this.usedSize-1; i >= pos ; i--) {
                this.elem[i+1] = this.elem[i];
            }
            this.elem[pos] = data;
            this.usedSize++;
        }
    }


    //判断是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.elem.length; i++) {
            if(this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    //查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.elem.length; i++) {
            if(this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    //获取下标 pos 位置的元素
    public int getVal(int pos) {
        if(this.usedSize == 0) {
            System.out.println("顺序表已为空！");
            return -1;
        }

        if(pos < 0 || pos > this.usedSize) {
            System.out.println(pos+"位置不合法！");
            return -1;
        }
        return this.elem[pos];
    }

    //给pos位置的元素设置为 val
    public void setVal(int pos, int val) {
        if(pos < 0 || pos > this.usedSize) {
            System.out.println(pos+"位置不合法！");
        } else {
            this.elem[pos] = val;
        }
    }

    //删除第一次出现的关键字 key
    public void removeKey(int key) {
        if(this.elem.length == 0) {
            return;  //顺序表为空 直接退出
        }
        boolean flg = false;
        int pos = 0;
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == key) {
                pos = i;
                flg = true;
                break;
            }
        }
        if(flg) {
            for (int i = pos+1; i < this.usedSize; i++) {
                this.elem[i-1]=this.elem[i];
            }
            this.usedSize--;
        }
    }

    //删除pos位置的数据
    public void deletePos(int pos) {

        if(this.usedSize == 0) { //为空直接退出
            return;
        }

        if(pos < 0 && pos > this.usedSize) {
            System.out.println("输入的pos：" + pos + "无效！删除失败！");
            return;
        }
        for (int i = pos+1; i < this.usedSize; i++) {
            this.elem[i-1] = this.elem[i];
        }
        this.elem[this.usedSize-1] = 0;
        this.usedSize--;
    }

    //获取顺序表长度
    public int getSize() {
        return this.usedSize;
    }

    //清空顺序表
    public void clear() {
        for (int i = this.usedSize - 1; i >= 0 ; i--) {
            this.elem[i] = 0;
            this.usedSize--;
        }
    }

}
